package com.aboo.action;

import com.aboo.po.Company;
import com.aboo.po.User;
import com.aboo.service.ICompanyService;
import com.aboo.service.RCompanyService;
import com.aboo.vo.ReturnVO;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @param
 * @author louan
 * @date 2021/4/21
 * @return
 **/
@Slf4j
@RestController
@RequestMapping("/user")
public class UserAction {

    @Resource
    private ICompanyService iCompanyService;
    @Resource
    private RCompanyService rCompanyService;

    @PostMapping("/get/{id}")
    public ReturnVO<User> get(@PathVariable("id") String id) {
        log.info("【用户】开始获取" + id);
        User user = new User();
        user.setId(id);
        user.setName("张三");
        user.setAge(11);
        log.info("【用户】获取成功" + user);

        log.info("===============================================================");
        ReturnVO<Company> rv1 = iCompanyService.get("1234");
        log.info("接口调用结果1:{}", JSONObject.toJSONString(rv1));
        log.info("===============================================================");

        log.info("===============================================================");
        ReturnVO<Company> rv2 = rCompanyService.get("5678");
        log.info("接口调用结果2:{}", JSONObject.toJSONString(rv2));
        log.info("===============================================================");

        return new ReturnVO<User>(user);
    }

    @PostMapping("/findList")
    public ReturnVO<List<User>> findList(User user) {
        log.info("【用户】开始获取列表" + user);
        List<User> list = new ArrayList<>();
        list.add(user);
        log.info("【用户】获取列表成功");
        return new ReturnVO<List<User>>(list);
    }

    @PostMapping("/add")
    public ReturnVO<User> add(@RequestBody User user) {
        log.info("【用户】开始添加" + user);
        user.setId(UUID.randomUUID().toString());
        log.info("【用户】添加成功" + user);
        return new ReturnVO<User>(user);
    }

    @PostMapping("/edit")
    public ReturnVO<User> edit(User user) throws Exception {
        // feign消费服务时，如果参数前什么也不写，那么会被默认是@RequestBody的
        log.info("开始编辑【用户】，{}", user);
        user.setAge(23);
        user.setName("Diyako");
        log.info("编辑后【用户】，{}", user);
        return new ReturnVO<>(user);
    }

}
